(function() {


    let types = '';
    template.defaults.imports.dateFormat = function(val) {

            if (val === 1) {
                types = '菜品分类'
            } else if (val === 2) {
                types = '套餐分类'
            }
            return types
        }
        //alert(11)

    // 渲染数据
    const q = {
        page: 1,
        pageSize: 10
    }
    renders()

    function renders() {

        $.ajax({
            method: 'get',
            url: 'http://121.4.102.44:8083/category/page',
            data: q,
            success(res) {
                // console.log(res);
                const htmlStr = template('tpl', res)
                $('tbody').html(htmlStr)
                sorter(res.data.total)
            }
        })

        //sorter(res.data.total)

    }
    let hlq_add = null;
    let hlq_edit = null;
    let hlq_foot = null;
    $('#hlq_btn').on('click', function() {
        hlq_add = layui.layer.open({
            type: 1,
            title: ['新增菜品分类', 'font-weight:700;font-size:18px;padding-top:10px;padding-bottom:50px'],
            area: ['600px', '400px'],
            //把模板标签的内容，作为弹出层的内容
            //content: $('#food-add').html()
            content: $('#food-add').html()
        })
    })
    $('.hlq_foots').on('reset', function(e) {
        e.preventDefault()
            // $('#food-add').hide()
        layui.layer.close(hlq_add)
    })
    $('#hlq_add').on('click', function() {
            hlq_foot = layui.layer.open({
                type: 1,
                title: ['新增套餐分类', 'font-weight:700;font-size:18px;padding-top:10px;padding-bottom:50px'],
                area: ['600px', '400px'],
                //把模板标签的内容，作为弹出层的内容
                content: $('#combo-add').html()
            })
        })
        //分页
    function sorter(total) {
        layui.laypage.render({
                //存放盒子的id，没有#
                elem: 'pageBox',
                //数据的总数
                count: total,
                //每页显示的条数
                limit: q.pageSize,
                prev: '<',
                next: '>',
                //当前的页码
                curr: q.page,
                //自定义分页按钮的排版
                layout: ['count', 'limit', 'prev', 'page', 'next', 'skip', ],
                //设置内页显示的条数（下拉列表选项）
                limits: [10, 20, 30, 40],
                jump: function(obj, first) {
                    //obj包含了当前分页的所有参数，比如：
                    //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    // console.log(obj.limit); //得到每页显示的条数
                    // console.log(first);
                    //首次不执行
                    if (!first) {
                        //do something
                        //修改查询参数q
                        q.page = obj.curr
                            //获取当前内页显示的条数
                        q.pageSize = obj.limit
                            //getArticleList()
                        renders()
                    }
                }
            })
            //console.log(q.pagesize);
    }
    //删除
    $('tbody').on('click', '#hlq_del', function() {
        let id = $(this).attr('data-id')
            // console.log(id);
        layui.layer.confirm('此操作将永久删除该文件, 是否继续?', {
            icon: 3,
            title: '提示'
        }, function(index) {
            $.ajax({
                method: 'DELETE',
                url: 'http://121.4.102.44:8083/category?id=' + id,
                success(res) {
                    //console.log(res);
                    if (res.code !== 1) {
                        return layui.layer.msg(res.msg, { icon: 5 })
                    }
                    layui.layer.msg('删除成功', { icon: 6 })
                    renders()
                }
            })
            layui.layer.close(index)
        })

    })

    //修改
    $('tbody').on('click', '#hlq_alter', function() {
        let id = $(this).attr('data-id')
        let name = $(this).parents('tr').children().eq(0).html()
        let sort = $(this).parents('tr').children().eq(3).html()
        hlq_edit = layui.layer.open({
            type: 1,
            title: ['修改分类', 'font-weight:700;font-size:18px;padding-top:10px;padding-bottom:50px'],
            area: ['600px', '400px'],
            //把模板标签的内容，作为弹出层的内容
            content: $('#combo-edit').html()
        })
        $('#form-edit [name="name"]').val(name)
        $('#form-edit [name="alias"]').val(sort)
        $('#form-edit [name="id"]').val(id)

    })
    $('body').on('submit', '#form-edit', function(e) {
        e.preventDefault()
        const name = $('#form-edit [name="name"]').val()
        const sort = $('#form-edit [name="alias"]').val()
        const id = $('#form-edit [name="id"]').val()

        $.ajax({
            method: 'put',
            url: 'http://121.4.102.44:8083/category',
            data: JSON.stringify({
                id,
                name,
                sort
            }),
            contentType: "application/json",

            success(res) {
                // console.log(res);
                if (res.code !== 1) {
                    return layui.layer.msg(res.msg, { icon: 5 })
                }
                layui.layer.msg('修改成功', { icon: 6, time: 1000 }, function() {
                    renders()
                })

            }
        })
        layui.layer.close(hlq_edit)
    })

    //新增菜品分类
    $('body').on('submit', '#form-add', function(e) {
        e.preventDefault()

        const name = $('#form-add [name="name"]').val()
        const sort = $('#form-add [name="alias"]').val()
            //console.log(name, sort);
        $.ajax({
            method: 'post',
            url: 'http://121.4.102.44:8083/category',
            data: JSON.stringify({
                name: name,
                sort: sort,
                type: 1
            }),
            contentType: "application/json",
            success(res) {

                // console.log(res);
                if (res.code !== 1) {
                    return layui.layer.msg(res.msg, { icon: 5 })
                }
                layui.layer.msg('添加成功', { icon: 6, time: 1000 }, function() {
                    $('#form-add [name="name"]').val('')
                    $('#form-add [name="alias"]').val('')
                    renders()
                })
            }
        })
        layui.layer.close(hlq_add)
        $('body').on('click', '#hlq_fd', function(e) {
            e.preventDefault()
            const name = $('#form-add [name="name"]').val()
            const sort = $('#form-add [name="alias"]').val()
            $.ajax({
                method: 'post',
                url: 'http://121.4.102.44:8083/category',
                data: JSON.stringify({
                    name: name,
                    sort: sort,
                    type: 2
                }),
                contentType: "application/json",
                success(res) {

                    console.log(res);
                    if (res.code !== 1) {
                        return layui.layer.msg(res.msg, { icon: 5 })

                    }
                    flag = true;
                    layui.layer.msg('添加成功', { icon: 6, time: 1000 }, function() {
                        $('#form-add [name="name"]').val('')
                        $('#form-add [name="alias"]').val('')
                        renders()
                    })
                }
            })

        })
    })


    //let flag = false;
    //新增套餐分类
    $('body').on('submit', '#form-combo', function(e) {

        e.preventDefault()
        const name = $('#form-combo [name="name"]').val()
        const sort = $('#form-combo [name="alias"]').val()
        $.ajax({
            method: 'post',
            url: 'http://121.4.102.44:8083/category',
            data: JSON.stringify({
                name: name,
                sort: sort,
                type: 2
            }),
            contentType: "application/json",
            success(res) {

                console.log(res);
                if (res.code !== 1) {
                    return layui.layer.msg(res.msg, { icon: 5 })

                }
                flag = true;
                layui.layer.msg('添加成功', { icon: 6, time: 1000 }, function() {
                    $('#form-combo [name="name"]').val('')
                    $('#form-combo [name="alias"]').val('')
                    renders()
                })
            }
        })
        layui.layer.close(hlq_foot)
        $('body').on('click', '#hlq_re', function(e) {
            e.preventDefault()
            const name = $('#form-combo [name="name"]').val()
            const sort = $('#form-combo [name="alias"]').val()
            $.ajax({
                method: 'post',
                url: 'http://121.4.102.44:8083/category',
                data: JSON.stringify({
                    name: name,
                    sort: sort,
                    type: 2
                }),
                contentType: "application/json",
                success(res) {

                    console.log(res);
                    if (res.code !== 1) {
                        return layui.layer.msg(res.msg, { icon: 5 })

                    }
                    flag = true;
                    layui.layer.msg('添加成功', { icon: 6, time: 1000 }, function() {
                        $('#form-combo [name="name"]').val('')
                        $('#form-combo [name="alias"]').val('')
                        renders()
                    })
                }
            })

        })


    })


})()